﻿using System;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class AuditionEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string strSql = @"with t as (select Id,Pid,cast(Text as nvarchar(500)) Text,OrderNo,1 lvl,cast(Id as nvarchar(50)) orderStr from Depts where Pid is null
union all
select d.Id,d.Pid,cast(REPLICATE('--',t.lvl)+d.Text as nvarchar(500)),
d.OrderNo,t.lvl+1,cast(t.orderStr+cast(d.Id as nvarchar(50)) as nvarchar(50))
from Depts d,t where d.Pid=t.id
)
  select Id,Text from t order by orderStr;";
                var depts = ctx.ExecuteQuery<DeptDTO>(strSql);
                Dept.DataSource = depts;
                Dept.DataValueField = "Id";
                Dept.DataTextField = "Text";
                Dept.DataBind();

                string id = Request.QueryString["id"];
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<AuditionMain>(r => r.AuditionSubs);
                ctx.LoadOptions = dlo;
                AuditionMain auditionMain = ctx.AuditionMains.Single(r => r.Id ==int.Parse(id));

                Name.Value = auditionMain.Name;
                Address.Value = auditionMain.Address;
                Dept.Value = auditionMain.DeptId.ToString();
                Position.Value = auditionMain.Position;
                Phone.Value = auditionMain.Phone;
                EMail.Value = auditionMain.EMail;
                InfoOrigin.Value = auditionMain.InfoOrigin;
                AuditionType.Value = auditionMain.AuditionType;
                Remarks.Value = auditionMain.Remarks;

                rptList.DataSource = auditionMain.AuditionSubs;
                rptList.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string id = Request.QueryString["id"];
            AuditionMain auditionMain = ctx.AuditionMains.Single(r => r.Id == int.Parse(id));
            auditionMain.Name = Name.Value;
            auditionMain.Address = Address.Value;
            auditionMain.DeptId = int.Parse(Dept.Value);
            auditionMain.Position = Position.Value;
            auditionMain.Phone = Phone.Value;
            auditionMain.EMail = EMail.Value;
            auditionMain.InfoOrigin = InfoOrigin.Value;
            auditionMain.AuditionType = AuditionType.Value;
            auditionMain.Remarks = Remarks.Value;

            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='" + auditionMain.Id + "';window.close();</script>");
        }
    }
}